package com.cloudcc.mobile.view.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.MessageNumX;
import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.cloudframe.util.DateUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dao.impl.ApprovalEngineImpl;
import com.cloudcc.mobile.db.PushSaveMessageDB;
import com.cloudcc.mobile.entity.PushSaveMessage;
import com.cloudcc.mobile.event.BeauEventList;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.messagecent.BeauTongzhi;
import com.cloudcc.mobile.messagecent.TongzhiImpl;
import com.cloudcc.mobile.service.MyPushIntentService;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.activity.BeauInfoActivity;
import com.cloudcc.mobile.view.activity.TaskManagementActivty;
import com.cloudcc.mobile.view.dynamic.DynamicActivity;
import com.cloudcc.mobile.view.mymodel.ParseJson;
import com.cloudcc.mobile.view.mymodel.my;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mypage.view.activity.ApprovalPendingActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xhsdele on 2017/8/4 0004.
 */

public class SlidingRightAdapter extends BaseAdapter{

//    protected List<PushSaveMessage> messsageList;
    private List<BeauTongzhi> tongzhil;
    private LayoutInflater inflater;
    private Context mContext;
    private PushSaveMessageDB db;
    private TongzhiImpl tongzhiImpl = new TongzhiImpl();
    private BeauEventList.BeauTongzhiEvent event;

    private long a =  RunTimeManager.getInstance().getMessageNx();
    PushSaveMessage mes = new PushSaveMessage();
//    public SlidingRightAdapter(List<PushSaveMessage> messsageList, Context mContext) {
//        this.messsageList = messsageList;
//        this.mContext = mContext;
//        inflater = LayoutInflater.from(mContext);
//        try {
//            db = new PushSaveMessageDB(mContext, UserManager
//                    .getManager().getUser().userId + "pushsavemessage");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    public SlidingRightAdapter(List<BeauTongzhi> tongzhil, Context mContext) {
        this.tongzhil = tongzhil;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return tongzhil.size();
    }

    @Override
    public Object getItem(int position) {
        return tongzhil.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null) {
            holder=new ViewHolder();
            convertView = inflater.inflate(R.layout.sliding_right_item, null);
            holder.textView = (TextView) convertView
                    .findViewById(R.id.cower_item_text);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.cower_image);
            holder.ll = (LinearLayout) convertView.findViewById(R.id.childll);
            holder.timex = (TextView) convertView.findViewById(R.id.time_xm);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(tongzhil.get(position).content);
        holder.timex.setText(DateUtils.howMuchDayPattern(mContext,tongzhil.get(position).createdate));
        if("0".equals(tongzhil.get(position).status)) {
            holder.ll.setBackgroundResource(R.color.messagebg);

        }else {
            holder.ll.setBackgroundResource(R.color.tzll);
        }

        final ViewHolder finalHolder = holder;
        if(tongzhil.get(position).userid==null) {

        }else {
        holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finalHolder.ll.setBackgroundResource(R.color.tzll);

                a--;
                RunTimeManager.getInstance().setMessageNx(a);
                EventEngine.post(new MessageNumX(a));
                event = new BeauEventList.BeauTongzhiEvent();
                tongzhiImpl.tongzhiNum(tongzhil.get(position).id,event);
//                long b = 0;
//                try {
//                    //消息数
//                    b = 0;
//                    if (db.isExit()) {
//                        db.updateIsNew(mes,tongzhil.get(position).getId());
//                        b = db.getNewPushSaveMessageNum();
//                    }
//                    EventEngine.post(new MessageNumX(b));
//                    RunTimeManager.getInstance().setMessageNx(b);
//                } catch (DbException e) {
//                    e.printStackTrace();
//                }
                if (tongzhil.get(position).function.equals("approval")) {// 审批通知
                    initTongzhi(tongzhil.get(position).relatedid);
                }
                if (tongzhil.get(position).function.equals("eventAlert")) {// 事件
                    Intent intent1 = new Intent(mContext, BeauInfoActivity.class);
                    intent1.putExtra("web.url", UrlManager.getRootUrl() + UrlTools.getObjectUrl_z(tongzhil.get(position).relatedid));
                    intent1.putExtra("web",tongzhil.get(position).relatedid);
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent1);
                }
                if (tongzhil.get(position).function.equals("taskAlert")) {// 任务
                    Intent intent1 = new Intent(mContext, BeauInfoActivity.class);
                    intent1.putExtra("web.url", UrlManager.getRootUrl() + UrlTools.getObjectUrl_z(tongzhil.get(position).relatedid));
                    intent1.putExtra("web",tongzhil.get(position).relatedid);
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent1);
                }
                if (tongzhil.get(position).function.equals("showmsg")) {// 云帖
                    Intent intent1 = new Intent(mContext, DynamicActivity.class);
                    intent1.putExtra("chatId", tongzhil.get(position).relatedid);
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent1);
                }

            }
        });
        }
        return convertView;
    }

    class ViewHolder{
        TextView textView;
        TextView timex;
        ImageView image;
        LinearLayout ll;
    }
    private my model;
    private ApprovalEngineImpl engine = new ApprovalEngineImpl();
    public void initTongzhi(String recordId){
        engine.GetManagementTask_z(RunTimeManager.getInstance().getUserId(),1,recordId, new ResultCallBack<JsonObject>() {
            @Override
            public void onSuccess(JsonObject t) {
                JsonObject zhaoyao = (JsonObject) t.get("zymap");
                // JsonElement jsonElement2 = t.get("zymap");
                Log.d("getfield", "解析下一步");
                Gson gson = new Gson();
                ParseJson json = gson.fromJson(t, ParseJson.class);
                // com.litesuits.android.log.Log.d("taskmodel",
                // json.resultList.get(0).fr);
                List<ParseJson.Zu> list = new ArrayList<ParseJson.Zu>();
                list = json.resultList;
                if(list.size()>0){
                    model = list.get(0).data.get(0);
                    Intent intent1 = new Intent(mContext, ApprovalPendingActivity.class);
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("extra", model);
                    intent1.putExtras(bundle);
                    mContext.startActivity(intent1);
                }else{
                    Intent intent1 = new Intent(mContext, TaskManagementActivty.class);
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent1);
                }
            }
            @Override
            public void onFailure(ErrorInfo error) {
                Intent intent1 = new Intent(mContext, TaskManagementActivty.class);
                mContext.startActivity(intent1);
            }
        });
    }



}

